﻿#include "reflecterlib.h"
#include <QDebug>
#include <QMessageBox>
#include <QThread>
#include "errcode.h"

int ReflecterLib::typeId = qRegisterMetaType<ReflecterLib*>("ReflecterLib*");

ReflecterLib::ReflecterLib()
{
    qDebug() << "Constractor was called!";
}

int ReflecterLib::testMethod0()
{
    qDebug() << "testMethod0 was called!";
    return ERR_OK;
}

int ReflecterLib::testMethod1(QVariant &intParam)
{
    if (!intParam.canConvert<int>())
        return ERR_PARAM;

    QString info = QString("testMethod1 was called, var1=%1.").arg(intParam.toInt());
    qDebug() << info;
    return ERR_OK;
}

int ReflecterLib::testMethod2(QVariant &intParam, QVariant &stringParam)
{
    if (!intParam.canConvert<int>())
        return ERR_PARAM;

    if (!stringParam.canConvert<QString>())
        return ERR_PARAM;

    intParam = 100;
    QThread::msleep(2000);

    QString info = QString("testMethod2 was called, var1=%1, var2=%2").arg(intParam.toInt()).arg(stringParam.toString());
    qDebug() << info;
    return ERR_OK;
}

int ReflecterLib::testMethod3(QVariant &intParam, QVariant &stringParam, QVariant &boolParam)
{
    if (!intParam.canConvert<int>())
        return ERR_PARAM;

    if (!stringParam.canConvert<QString>())
        return ERR_PARAM;

    if (!boolParam.canConvert<bool>())
        return ERR_PARAM;

    QThread::msleep(2000);

    QString info = QString("testMethod3 was called, var1=%1, var2=%2, var3=%3")
            .arg(intParam.toInt())
            .arg(stringParam.toString())
            .arg(boolParam.toBool());
    qDebug() << info;
    return ERR_OK;
}

int ReflecterLib::testMethod4(QVariant &intParam, QVariant &stringParam, QVariant &boolParam, QVariant &byteArrayParam)
{
    if (!intParam.canConvert<int>())
        return ERR_PARAM;

    if (!stringParam.canConvert<QString>())
        return ERR_PARAM;

    if (!boolParam.canConvert<bool>())
        return ERR_PARAM;

    if (!byteArrayParam.canConvert<QByteArray>())
        return ERR_PARAM;

    QThread::msleep(2000);

    QString info = QString("testMethod4 was called, var1=%1, var2=%2, var3=%3, var4=%4")
            .arg(intParam.toInt())
            .arg(stringParam.toString())
            .arg(boolParam.toBool())
            .arg(byteArrayParam.toByteArray().toHex(' ').data());
    qDebug() << info;
    return ERR_OK;
}
